<?php

namespace app\api\controller;

use addons\epay\library\Service;
use app\admin\model\jeek\Attestation;
use app\common\controller\Api;
use app\common\model\CopartnerPaylog;
use app\common\model\Order;
use app\common\model\UserVip;

/**
 * 支付回调接口
 */
class Pay extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    /**
     * Account:回调方法
     * User: Ixiangang
     * Date: 2025/1/18
     * Time: 13:40
     */
    public function demo_notify()
    {
        $out_trade_no = $this->request->post('out_trade_no');
        $payamount = $this->request->post('payamount');
        $pay_type = $this->request->post('pay_type');
        $prefix = explode('_', $out_trade_no)[0] ?? '';
        switch ($prefix) {
            # 咨询师认证支付
            case 'R':
                $jeekAttestation = new Attestation();
                $jeekAttestation->notify($out_trade_no, $payamount, $pay_type);
                break;
            # 用户下单
            case 'SN':
                $order = new Order();
                $order->notify($out_trade_no, $payamount, '', $pay_type);
                break;
            # 充值VIP
            case 'V':
                $userVip = new UserVip();
                $userVip->notify($out_trade_no, $payamount, $payamount, $pay_type);
                break;
            # 合伙人缴费
            case 'H':
                $copartnerp = new CopartnerPaylog();
                $copartnerp->notify($out_trade_no, $payamount, $payamount, $pay_type);
                break;
            default:
                break;
        }
    }



    /**
     * Account:支付回调方法
     * User: Ixiangang
     * Date: 2025/1/18
     * Time: 11:16
     */
    public function notify()
    {
        $paytype = $this->request->param('paytype');
        $pay = Service::checkNotify($paytype);
        if (!$pay) {
            return json(['code' => 'FAIL', 'message' => '失败'], 500, ['Content-Type' => 'application/json']);
        }
        // 获取回调数据，V3和V2的回调接收不同
        $data = Service::isVersionV3() ? $pay->callback() : $pay->verify();
        \think\Log::record("回调逻辑处理错误查询data:" . $data);
        try {
            //微信支付V3返回和V2不同
            if (Service::isVersionV3() && $paytype === 'wechat') {
                $data = $data['resource']['ciphertext'];
                $data['total_fee'] = $data['amount']['total'];
            }
            \think\Log::record($data);
            //获取支付金额、订单号
            $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
            //回调订单编号
            $pay_sn = $paytype == 'alipay' ? $data['trade_no'] : $data['transaction_id'];
            //支付订单编号
            $out_trade_no = $data['out_trade_no'];
            $prefix = explode('_', $out_trade_no)[0] ?? '';
            switch ($prefix) {
                # 咨询师认证支付
                case 'R':
                    $jeekAttestation = new Attestation();
                    $jeekAttestation->notify($out_trade_no, $payamount, $pay_sn, $paytype);
                    break;
                # 用户下单
                case 'SN':
                    $order = new Order();
                    $order->notify($out_trade_no, $payamount, $pay_sn, $paytype);
                    break;
                # 充值VIP
                case 'V':
                    $userVip = new UserVip();
                    $userVip->notify($out_trade_no, $payamount, $pay_sn, $paytype);
                    break;
                # 合伙人缴费
                case 'H':
                    $copartnerp = new CopartnerPaylog();
                    $copartnerp->notify($out_trade_no, $payamount, $pay_sn, $paytype);
                    break;
                default:
                    break;
            }
            \think\Log::record("回调成功，订单号：{$out_trade_no}，金额：{$payamount}");
            //你可以在此编写订单逻辑
        } catch (\Exception $e) {
            \think\Log::record("回调逻辑处理错误:" . $e->getMessage());
        }
        //下面这句必须要执行,且在此之前不能有任何输出
        if (Service::isVersionV3()) {
            return $pay->success()->getBody()->getContents();
        } else {
            return $pay->success()->send();
        }
    }


}